package org.jeecg.modules.music.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.*;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.music.entity.MusArtist;
import org.jeecg.modules.music.entity.MusUser;
import org.jeecg.modules.music.service.IMusUserService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysLogService;
import org.jeecg.modules.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags="音乐-用户管理")
@RestController
@RequestMapping("/mus/user")
public class MusUserController {

    @Autowired
    private RedisUtil redisUtil;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    @Value("${music.password}")
    private String password;

    @Autowired
    private IMusUserService musUserService;
    @Resource
    private BaseCommonService baseCommonService;
    @Autowired
    private IMusUserService sysUserService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private ISysLogService logService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private ISysTenantService sysTenantService;
    @Autowired
    private ISysDictService sysDictService;


    @ApiOperation("MusUser-列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<MusUser>> queryPageList(MusUser user, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MusUser>> result = new Result<IPage<MusUser>>();
        QueryWrapper<MusUser> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());
        Page<MusUser> page = new Page<MusUser>(pageNo, pageSize);
        IPage<MusUser> pageList = musUserService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @ApiOperation("MusUser-添加")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<MusUser> add(@RequestBody JSONObject jsonObject){
        Result<MusUser> result = new Result<MusUser>();

        try{
            MusUser user = JSON.parseObject(jsonObject.toJSONString(),MusUser.class);
            user.setCreateTime(new Date());
            user.setStatus(1);
            musUserService.saveMusUser(user);
            result.success("添加成功！");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("MusUser-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<MusUser> edit(@RequestBody JSONObject jsonObject) {
        Result<MusUser> result = new Result<MusUser>();
        try {
            MusUser user = musUserService.getById(jsonObject.getString("id"));
            baseCommonService.addLog("编辑用户，id： " +jsonObject.getString("id") ,CommonConstant.LOG_TYPE_2, 2);
            if(user==null) {
                result.error500("未找到对应实体");
            }else {
                MusUser musUser = JSON.parseObject(jsonObject.toJSONString(), MusUser.class);
                musUser.setUpdateTime(new Date());
                musUserService.updateUser(musUser);
                result.success("修改成功!");
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        baseCommonService.addLog("删除歌手，id： " +id ,CommonConstant.LOG_TYPE_2, 3);
        this.musUserService.deleteMusUser(id);
        return Result.ok("删除成功");
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        baseCommonService.addLog("批量删除， ids： " +ids ,CommonConstant.LOG_TYPE_2, 3);
        this.musUserService.deleteBatchMusUser(ids);
        return Result.ok("批量删除用户成功");
    }

    @RequestMapping(value = "/reset/password",method = RequestMethod.GET)
    public Result<?> resetPassword(@RequestParam(name = "uid",required = true) String uid){
        this.musUserService.resetUserPassword(uid,password,new Date());
        return Result.ok("初始密码成功！");
    }
}
